import Vue from "vue";
const shopPlugin = require('../../static/shopPlugin/api.js')
export default {
	namespaced: true, // 命名空间必须写
	state: {
		systemConfig: {}, // 系统配置
		spreadInfo: {}, // 平台配置
		categoryId: '',
		toText: {}, // 转文字
		isShowToastClick: true, // 气泡框是否点击
		subTemplateIds: [], // 首页订阅消息模板id
		giftActivitiesList: [],
		isSettings: false, //微信小程序订阅消息
		agent_apply: 0,
		enterprises_auth_apply: -1,
		spreadToken: '',
		order_comment_img_number: 1,
		order_comment_status: 1,
		agent_apply_is_notify: null,
		distribution_apply_pass_img: '', //代理通过权益图片
		isCouponRemind: true,
		order_pay_after_img: '', //支付完成后客服二维码
		popNumber: 0, // 下单弹框次数
		productId: 0, //商品id
		isShare: 0,
		initLoading: true,
		navbarHeight: uni.getStorageSync('navbarHeight') || 0 //导航栏高度
	},
	getters: {
		getState(state) {
			return state;
		}
	},
	// 唯一修改state值的方法 
	// commit
	mutations: {
		// 首页保存分类下标
		setCategory(state, val) {
			state.categoryId = val
		},
		setSystemConfig(state, value) {
			state.systemConfig = value;
		},
		setSpreadInfo(state, value) {
			state.spreadInfo = value;
		},
		setGiftActivities(state, value) {
			state.giftActivitiesList = value;
		},
		setToText(state, value) {
			state.toText = value;
		},
		setIsShowToastClick(state, value) {
			state.isShowToastClick = value;
		},
		setsubTemplateIds(state, val) {
			state.subTemplateIds = Object.values(val)
		},
		setSettings(state, val) {
			state.isSettings = val
		},
		setAllocation(state, val) {
			state.agent_apply = val.agent_apply
			state.enterprises_auth_apply = val.enterprises_auth_apply
		},
		setSpreadToken(state, val) {
			state.spreadToken = val
		},
		setComment(state, val) {
			state.order_comment_img_number = val.systemConfig.order_comment_img_number
			state.order_comment_status = val.systemConfig.order_comment_status
			state.agent_apply_is_notify = val.agent_apply_is_notify
			state.distribution_apply_pass_img = val.systemConfig.distribution_apply_pass_img
			state.order_pay_after_img = val.systemConfig.order_pay_after_img
		},
		setAgentApplyIsNotify(state, val) {
			state.agent_apply_is_notify = val
		},
		setCouponRemind(state, val) {
			state.isCouponRemind = val
		},
		setPopNumber(state, val) {
			state.popNumber = val
		},
		setProduct_id(state, val) {
			state.productId = val
		},
		setIsShare(state, val) {
			state.isShare = val
		},
		seInitLoading(state, val) {
			state.initLoading = val
		},
		setNavbarHeight(state, val) {
			state.navbarHeight = val
			uni.setStorageSync('navbarHeight', state.navbarHeight);
		},
	},
	// 异步的操作
	actions: {
		// 获取初始化信息
		getInit({
			state,
			commit,
			dispatch,
			rootState
		}) {
			// #ifdef MP-WEIXIN
			uni.login({
				provider: 'weixin',
				success: function(loginRes) {
					let params = {
						user_terminal: 'weapp',
						code: loginRes.code,
						appid: Vue.prototype.$wxMinAppid,
					}
					Vue.prototype.$post("index/initData", params).then(res => {
						let data = res.data;
						let token = data.token.token_type + ' ' + data.token.access_token;
						uni.setStorageSync('token', token)
						// 跨境登录
						let code = -1
						if (data.token && data.token.yueyang &&
							data.token.user_type == 1) {
							const user_info = data.token.yueyang.user_info
							const sign_info = data.token.yueyang.sign_info
							const openid = data.token.yueyang.user_uuid
							shopPlugin.ssoPlugin(openid, user_info, sign_info, true, (ret) => {
								code = ret
								if (ret < 0) {
									console.log('跨境登录失败！')
									// Vue.prototype.$removeAllUserStorage();
									shopPlugin.logout()
									// setTimeout(function() {
									// 	store.dispatch('init/getInit')
									// 	Vue.prototype.$toLogin();
									// }, 1000)
								}
							});
							commit('user/setIsLogin', true, {
								root: true
							});
							// 用户已登录
							commit('user/setUserType', data.token.user_type, {
								root: true
							});
							// 获取用户信息
							dispatch('user/getUserInfo', 1, {
								root: true
							});
						} else {
							commit('user/setIsLogin', false, {
								root: true
							});
						}
						// 存储配置信息
						commit('seInitLoading', false);
						commit('setAllocation', data);
						// 存储系统配置信息
						commit('setSystemConfig', data.systemConfig);
						// 存储平台信息
						commit('setSpreadInfo', data.spreadInfo);
						// 存储赠品
						commit('setGiftActivities', data.giftActivitiesList);
						// 获取隐私token
						commit('setSpreadToken', data.spread_token);
						// 存储转文字信息
						commit('setToText', data.toText);
						// 存储评论相关信息
						commit('setComment', data);
						// 订阅消息：首页模板id
						if (data.subTemplateIds) {
							commit('setsubTemplateIds', data.subTemplateIds);
						}
						let isAgent = data.systemConfig.index_agent_show == 1 ? 'agent' :
							'notAgent'
						commit('tabbar/setIsAgent', isAgent, {
							root: true
						});
						// 进入页面onLond
						Vue.prototype.$isResolve()
					}).catch(ex => {
						console.log(ex, 'ex')
						Vue.prototype.$errorToast(ex);
					})
				}
			})
			// #endif
		},
		// 获取二维码信息
		getQrCodeParams({
			state,
			commit,
			dispatch,
			rootState
		}, val) {
			if (!val) return;
			Vue.prototype.$get("index/getQrCodeParamsV2", {
				qrcode: val,
				qrcode_token: val,
			}).then(res => {
				if (res.code == 200) {
					// 二维码参数存入缓存
					const qrCodeParams = res.data.params;
					uni.setStorageSync('parent_id', qrCodeParams.parent_id)
					commit('setProduct_id', qrCodeParams.product_id || 0)
					commit('setIsShare', qrCodeParams.is_share || 0)
				} else {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						confirmText: '我知道了',
					});
				}
			}).catch(ex => {
				Vue.prototype.$errorToast();
			})
		},
	}
}